from sqlalchemy import Column, Integer, String, Text, ForeignKey, TIMESTAMP, Boolean,JSON
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from database import Base

# 用户表
class LearnUser(Base):
    __tablename__ = "learn_user"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, nullable=False)
    email = Column(String, unique=True, nullable=False)
    password_hash = Column(Text, nullable=False)
    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())

    # 关系
    page_records = relationship("LearnPageRecord", back_populates="user")
    materials = relationship("LearnMaterial", back_populates="user")
    material_records = relationship("LearnUserMaterialRecord", back_populates="user")
    mind_maps = relationship("LearnMindMap", back_populates="user", cascade="all, delete-orphan")
    knowledge_cards = relationship("LearnknowledgeCard", back_populates="user", cascade="all, delete-orphan")

    def check_password(self, password: str):
        return self.password_hash == password

# 学习材料表
class LearnMaterial(Base):
    __tablename__ = "learn_material"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("learn_user.id", ondelete="CASCADE"))
    title = Column(Text, nullable=False)
    file_path = Column(Text, nullable=False)
    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())

    # 关系
    user = relationship("LearnUser", back_populates="materials")
    page_records = relationship("LearnPageRecord", back_populates="material", cascade="all, delete-orphan")
    material_records = relationship("LearnUserMaterialRecord", back_populates="material", cascade="all, delete-orphan")
    mind_maps = relationship("LearnMindMap", back_populates="material", cascade="all, delete-orphan")
    knowledge_cards = relationship("LearnknowledgeCard", back_populates="material", cascade="all, delete-orphan")

# 阅读记录表，每一条记录代表阅读过后的页
class LearnPageRecord(Base):
    __tablename__ = "learn_page_record"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("learn_user.id", ondelete="CASCADE"))
    page_number = Column(Integer, nullable=False)
    material_id = Column(Integer, ForeignKey("learn_material.id", ondelete="CASCADE")) 
    stone = Column(Boolean, default=False) # 是否是默认插入的记录。
    mind_map_status = Column(Boolean, default=False) # 是否用于生成思维导图
    card_status = Column(Boolean, default=False) # 是否用于生成知识卡片

    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())

    # 关系
    user = relationship("LearnUser", back_populates="page_records")
    material = relationship("LearnMaterial", back_populates="page_records")

# 知识卡片表
class LearnknowledgeCard(Base):
    __tablename__ = "learn_knowledge_card"
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("learn_user.id", ondelete="CASCADE"))
    material_id = Column(Integer, ForeignKey("learn_material.id", ondelete="CASCADE"))
    title = Column(Text, nullable=False)
    summary = Column(Text, nullable=False)
    html_content = Column(Text, nullable=False)
    page_number = Column(Text, nullable=False)

    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())

    # 关系
    user = relationship("LearnUser", back_populates="knowledge_cards")
    material = relationship("LearnMaterial", back_populates="knowledge_cards")

# 用户学习记录表
class LearnUserMaterialRecord(Base):
    __tablename__ = "learn_user_material_record"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("learn_user.id", ondelete="CASCADE"))
    material_id = Column(Integer, ForeignKey("learn_material.id", ondelete="CASCADE"))
    page_number = Column(Integer, nullable=False)

    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())

    # 关系
    user = relationship("LearnUser", back_populates="material_records")
    material = relationship("LearnMaterial", back_populates="material_records")

# 用户思维导图表
class LearnMindMap(Base):
    __tablename__ = "learn_mind_map"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("learn_user.id", ondelete="CASCADE"))
    material_id = Column(Integer, ForeignKey("learn_material.id", ondelete="CASCADE"))
    mind_map_json = Column(JSON, nullable=False)  # 存储 jsMind 结构化数据
    version = Column(Integer, default=1)  # 版本号，确保更新时有记录
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())  # 记录更新时间

    # 关系
    user = relationship("LearnUser", back_populates="mind_maps")
    material = relationship("LearnMaterial", back_populates="mind_maps")
    
    
